<template>
  <el-dialog
    :title="!dataForm.catIdentify ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="猫的编号" prop="catSickId">
      <el-input v-model="dataForm.catSickId" placeholder="猫的编号"></el-input>
    </el-form-item>
    <el-form-item label="生病日期" prop="catSickDate">
      <el-input v-model="dataForm.catSickDate" placeholder="生病日期"></el-input>
    </el-form-item>
    <el-form-item label="生病类型" prop="catSickType">
      <el-input v-model="dataForm.catSickType" placeholder="生病类型"></el-input>
    </el-form-item>
    <el-form-item label="负责人" prop="catSickCarer">
      <el-input v-model="dataForm.catSickCarer" placeholder="负责人"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="catSickDesc">
      <el-input v-model="dataForm.catSickDesc" placeholder="描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          catIdentify: 0,
          catSickId: '',
          catSickDate: '',
          catSickType: '',
          catSickCarer: '',
          catSickDesc: ''
        },
        dataRule: {
          catSickId: [
            { required: true, message: '猫的编号不能为空', trigger: 'blur' }
          ],
          catSickDate: [
            { required: true, message: '生病日期不能为空', trigger: 'blur' }
          ],
          catSickType: [
            { required: true, message: '生病类型不能为空', trigger: 'blur' }
          ],
          catSickCarer: [
            { required: true, message: '负责人不能为空', trigger: 'blur' }
          ],
          catSickDesc: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.catIdentify = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.catIdentify) {
            this.$http({
              url: this.$http.adornUrl(`/cat/sickrecord/info/${this.dataForm.catIdentify}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.catSickId = data.sickRecord.catSickId
                this.dataForm.catSickDate = data.sickRecord.catSickDate
                this.dataForm.catSickType = data.sickRecord.catSickType
                this.dataForm.catSickCarer = data.sickRecord.catSickCarer
                this.dataForm.catSickDesc = data.sickRecord.catSickDesc
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cat/sickrecord/${!this.dataForm.catIdentify ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'catIdentify': this.dataForm.catIdentify || undefined,
                'catSickId': this.dataForm.catSickId,
                'catSickDate': this.dataForm.catSickDate,
                'catSickType': this.dataForm.catSickType,
                'catSickCarer': this.dataForm.catSickCarer,
                'catSickDesc': this.dataForm.catSickDesc
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
